{template header}
<!--<form class="form-horizontal"  action="?c=adminErp&a=addErpProduct">-->
   <input type="hidden" id='pid' name="pid" value="{$pid}">
    <!--规格开始-->

    <h4 style="padding-left:10px;;">
        <a href="?c=adminErp&a=getErpProductList"><span>库存列表</span></a>
        >>
        <a href="?c=adminErp&a=addErpProStandard&pid={$pid}"><span>规格管理</span></a>
        <span class="btn btn-primary" id="addGg" >添加规格</span>
    </h4>
    <div class="control-group">
        <label class="control-label" >货品名：</label>
        <div class="controls" style="margin: -30px 10px 10px 60px;">
            <a class="btn" href="#dialogProductTitle" data-toggle="modal" style="line-height: 35px;">{$productInfo['title']}</a>
        </div>
    </div>

    <div id="gg_content">
        {loop $info $k $v}
        <div class="control-group" id="gg_key">
            <button class="control-label btn btn-warning editHead" data-id="{$v['id']}">{$v['title']}</button>：
            <span class="btn btn-primary addGgVal" headId ="{$v['id']}"  >添加规格值</span>
            {if $k==0}
            <span style="color:rebeccapurple">*建议控制在10个汉字长度之内，标签名控制在5个汉字以内，个数不超过10个。规格图片尺寸建议为等比宽高,如:100x100,200x200</span>
            {/if}
            <div class="controls">
              {if isset($v['child'])}
              <ul class="inline">
                {loop $v['child'] $k2 $v2}
                  <li style="width: 180px;">
                    <button class="btn editVal" style="margin: 10px 10px 10px 10px;" data-id="{$v2['id']}" data-headid ="{$v['id']}" >{$v2['title']}</button>
                    {if $k==0}
                    <form id="imgForm{$v2['id']}" enctype="multipart/form-data" method="post">
                    <img id="img{$v2['id']}" width="100" style="width:100px;height:100px;" {if !$v2['img_source']}style="display:none"{/if} src="{$v2['img_source']}">
                    <input type="hidden" name="attrValueId" value="{$v2['id']}">
                    <input type="file" class="imgFile" name="imgFile" data-id="{$v2['id']}">
                    </form>
                    {/if}
                  </li>
                {/loop}
              </ul>
              {/if}
            </div>
        </div>
        {/loop}

    </div>
    {if count($info)>0}
    <h4 style="padding-left:10px;;">
        <div style="margin: 10px 10px 10px 10px;">
            <span>入库货品</span> <span class="btn btn-primary" id="addRk" >添加</span>
        </div>
        <form action="?c=adminErp&a=addGoods" method="POST" id="form2">
        <table style="width: 100%" border="1" id="table">
            <thead>
                <tr>
                    {loop $info $k $v}
                    <td>{$v['title']}</td>
                    {/loop}
                    <td>入库数量</td>
                    <td>货品成本</td>
                    <td>总价</td>
                    <td>操作</td>
                </tr>
            </thead>
        </table>
        </form>
    </h4>

    <span class="btn btn-primary" id="addRkOk" >确定入库</span>
    {/if}
<!--</form>-->

<div  id="addContent" style="display: none">
    <table>
        {loop $info_add $k $v}
        <tr>
            {loop $v $k2 $v2}
        <td>
            <select name="standValId[]">
                    <option value="{$v2['id']}">{$v2['title']}</option>
            </select>
        </td>
            {/loop}
        <td><input type="text" name="stockIntoCnt[]" class="stockIntoInput"  value="1"  /></td>
        <td><input type="text" name="price[]" class="priceInput" value="0" /></td>
        <td><input type="text" readonly value="0" class="totalPriceInput" /></td>
        <td><a href="javascript:void(0);" class="delNew">删除</a></td>
    </tr>
        {/loop}
    </table>
</div>


<div id="dialogProductTitle" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>货品名</h3>
  </div>
    <div class="modal-body">
        <span>规格名：</span><input type="text" id="productTitle" value="{$productInfo['title']}" placeholder="货品名称" /><br />
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" id="submitProductTitle" data-pid="{$pid}" data-id="">确定</button>
        <button class="btn" data-dismiss="modal" >取消</button>
    </div>
</div>
<div id="dialogHead" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>规格名</h3>
  </div>
    <div class="modal-body">
        <span>规格名：</span><input type="text" value="" id="remarkHead" placeholder="规格名称" /><br />
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" id="submitHead" data-pid="{$pid}" data-id="">确定</button>
        {if $editable}
        <button class="btn btn-danger" id="delHead" data-id="">删除</button>
        {/if}
        <button class="btn" data-dismiss="modal" >取消</button>
    </div>
</div>
<div id="dialogVal" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>规格值</h3>
  </div>
    <div class="modal-body">
        <span>规格值：</span><input type="text" value="" id="remarkVal" placeholder="多个规格值用|分隔" /><br />
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" id="submitVal" data-id="" data-headid="">确定</button>
        {if $editable}
        <button class="btn btn-danger" id="delVal" data-id="">删除</button>
        {/if}
        <button class="btn" data-dismiss="modal" >取消</button>
    </div>
</div>
<script type="text/javascript">
    $('#addRk').on('click',function(){
        $('#table').append($('#addContent table').html());
    })

    /*删除*/
    $(document).on('click',".delNew",function(){
        var _this=$(this),_tr=_this.parent().parent();
        _tr.remove();
    })

    /*入库数量 单价联动*/
    $(document).on('focus keyup keydown','.stockIntoInput',function(){
        var _this=$(this),
                _priceInput = _this.parent().siblings().find('.priceInput'),
                _totalPriceInput = _this.parent().siblings().find('.totalPriceInput');
        var reg = new RegExp("^([1-9]+)([0-9]*)$"),reg2 = new RegExp(/^[0-9]+(\.[0-9]{1,2})?$/);
        if (!reg.test(_this.val())) {
            _totalPriceInput.val(0);
            return;
        }
        if (!reg2.test(_priceInput.val())) {
            _totalPriceInput.val(0);
            return;
        }
        var _total = _this.val()*_priceInput.val();
        _totalPriceInput.val(_total.toFixed(2))
    })

    $(document).on('focus keyup keydown','.priceInput',function(){
        var _this=$(this),
                _stockIntoInput = _this.parent().siblings().find('.stockIntoInput'),
                _totalPriceInput = _this.parent().siblings().find('.totalPriceInput');
        var reg = new RegExp("^([1-9]+)([0-9]*)$"),reg2 =  new RegExp(/^[0-9]+(\.[0-9]{1,2})?$/);
        console.log(reg2.test(_this.val()));
        if (!reg2.test(_this.val())) {
            _totalPriceInput.val(0);
            return;
        }
        if (!reg.test(_stockIntoInput.val())) {
            _totalPriceInput.val(0);
            return;
        }
        var _total = _this.val()*_stockIntoInput.val();
        _totalPriceInput.val(_total.toFixed(2))
    })

    $('#addRkOk').click(function () {
        $.ajax({
            type:'POST',
            url:$('#form2').attr('action'),
            data:$('#form2').serialize()+'&pid='+$('#pid').val(),
            dataType:'json',
            success:function(d){
                if(d.status=='ok'){
                    alert(d.error_msg);
                    window.location.href= d.url;
                }else{
                    alert(d.error_msg);
                }
            }
        })
    })
    

    $('#addGg').on('click',function(){
        $('#submitHead').data('id','')
        $('#delHead').hide()
        $('#dialogHead').modal('show')
    })
    
    $('.editHead').click(function(){
        $('#submitHead').data('id',$(this).data('id'))
        $('#delHead').data('id',$(this).data('id'))
        $('#delHead').show()
        $('#dialogHead').modal('show')
    })

    $(".addGgVal").on('click',function(){
        $('#submitVal').data('id','')
        $('#submitVal').data('headid',$(this).attr('headId'));
        $('#delVal').hide()
        $('#dialogVal').modal('show')
    })
    
    $('.editVal').click(function(){
        $('#submitVal').data('id',$(this).data('id'))
        $('#submitVal').data('headid',$(this).data('headid'));
        $('#delVal').data('id',$(this).data('id'))
        $('#delVal').show()
        $('#dialogVal').modal('show')
    })
    
    $('#submitHead').click(function(){
        var remark = $('#remarkHead').val()
        if (!remark) {
            alert('请填写内容');
            return false;
        }
        var id = $(this).data('id')
        var url = id ? '?c=adminErp&a=updateStandardHead' : '?c=adminErp&a=addStandardHead'
        $.ajax({
            type:'POST',
            url: url,
            data:{pid:$(this).data('pid'),name:remark,id:id},
            dataType:'json',
            success:function(d){
                if(d.status=='ok'){
                    alert(d.error_msg);
                    window.location.reload();
                }else{
                    alert(d.error_msg);
                }
            }
        })
    })
    
    $('#submitVal').click(function(){
        var remark = $('#remarkVal').val()
        if (!remark) {
            alert('请填写内容');return false;
        }
        var arrRemark = remark.split('|');
        if (arrRemark.length <=0) {
            alert('请填写内容');return false;
        }
        var id = $(this).data('id')
        var url = id ? '?c=adminErp&a=updateStandardVal' : '?c=adminErp&a=addStandardVal'
        $.ajax({
            type:'POST',
            url: url,
            data:{headId:$(this).data('headid'),strStandVal:remark,id:id},
            dataType:'json',
            success:function(d){
                if(d.status=='ok'){
                    alert(d.error_msg);
                    window.location.reload();
                }else{
                    alert(d.error_msg);
                }
            }
        })
    })
    
    $('#submitProductTitle').click(function(){
        var remark = $('#productTitle').val()
        if (!remark) {
            alert('请填写内容');return false;
        }
        var id = $(this).data('pid')
        $.post('?c=adminErp&a=updateErpProduct',{id:id,title:remark},function(d){
            d = JSON.parse(d)
            if(d.status=='ok'){
                alert(d.error_msg);
                window.location.reload();
            }else{
                alert(d.error_msg);
            }
        })
    })
    
    $('.imgFile').change(function(){
      var id = $(this).data('id')
      var data = new FormData(document.getElementById('imgForm'+id));
      $.ajax({
        type: 'POST',
        url: '?c=adminErp&a=ajaxUploadImage',
        data: data,
        processData: false,
        contentType: false,
        dataType: 'json',
        success: function(data){
          $('#img'+id).attr('src',data.image)
          $('#img'+id).show()
        }
      })
    })
    
    $('#delHead').click(function(){
      var id = $(this).data('id')
      if(!id){
        alert('id 错误')
      }
      $.post('/?c=adminErp&a=delStandardHead',{id:id},function(data){
        data = JSON.parse(data)
        alert(data.error_msg)
        if(data.status == 'ok'){
          window.location.reload();
        }
      })
    })
    
    $('#delVal').click(function(){
      var id = $(this).data('id')
      if(!id){
        alert('id 错误')
      }
      $.post('/?c=adminErp&a=delStandardVal',{id:id},function(data){
        data = JSON.parse(data)
        alert(data.error_msg)
        if(data.status == 'ok'){
          window.location.reload();
        }
      })
    })
</script>
{template footer}
